//引入express模块
const express=require('express');
//引入连接池模块
const pool=require('../pool.js');
//创建路由器对象
const r=express.Router();
//1.用户注册
r.post("/reg",(req,res)=>{
	//获取流传递的数据
	var obj=req.body;
	var sql="insert into vip_user set?";
	pool.query(sql,[obj],(err,result)=>{
	  if(err) throw err;
	  if(result.affectedRows>0){
	  	res.send("注册成功")
	  }else{
	  	res.send("注册失败")
	  }
	})
 });
 //2.用户登录(get  /login/用户名&密码)
r.get("/login/:uname&:upwd",(req,res)=>{
	var $uname=req.params.uname;
	var $upwd=req.params.upwd;
	var sql="select * from vip_user where uname=? and upwd=?";
	pool.query(sql,[$uname,$upwd],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send("登录成功")
		}else{
			res.send("登录失败")
		}
	})
});
 
 //3.修改用户(put )
 r.put("/update/:uid&:uname",(req,res)=>{
    var obj=req.params;
    var sql="update vip_user set? where uid=?"
    pool.query(sql,[obj,obj.uid],(err,result)=>{
	    if(err) throw err;
		if(result.affectedRows>0){
			res.send("修改成功")
    	}else{
   			res.send("修改失败")
    	}
    })
 });
 
 //4.删除用户
 r.delete("/del/:uid",(req,res)=>{
	var $uid=req.params.uid;
	var sql="delete from vip_user where uid=?"
	pool.query(sql,[$uid],(err,result)=>{
		if(err) throw err;
		if(result.affectedRows>0){
			res.send("删除成功")
		}else{
			res.send("删除失败")
		}
	})
})
 //5.查找用户(get /编号)
r.get("/search/:uid",(req,res)=>{
	var $uid=req.params.uid;
	var sql="select * from vip_user where uid=?";
	pool.query(sql,[$uid],(err,result)=>{
		if(err) throw err;
		if(result.length>0){
			res.send(result);
		}else{
			res.send("查找失败");
		}
	});
})
//导出路由器对象
module.exports=r;